<?php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class purchase_order extends CI_Controller {
	function __construct()
	{
			parent::__construct();
		// check if salesman logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		$this->load->library('form_validation');
		$this->load->model('purchase/purchase_model');
		//$this->load->model('people/salesman_model');		
		$this->load->library('ion_auth');
		$this->load->library('session');
		
		$this->load->library('form_validation');
		$this->load->helper('url');

		// Load MongoDB library instead of native db driver if required
		$this->config->item('use_mongodb', 'ion_auth') ?
		$this->load->library('mongo_db') :

		$this->load->database();
		$this->load->library('datatables');

		$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));



	}
	
	function get(){
		echo $this->purchase_model->get();
	}
		
	function new_order(){
			//echo "i am here";exit;
			if (!$this->ion_auth->logged_in())
			{
				redirect('auth', 'refresh');
			}

		$this->form_validation->set_rules('referance_no', 'Referance Number', 'required|xss_clean');
		$this->form_validation->set_rules('received_date', 'Received date', 'required|xss_clean');
		$this->form_validation->set_rules('id_supplier', 'Supplier', 'required|xss_clean');
			$this->data['taxes']=$this->purchase_model->get_tax();
			$this->data['suppliers']=$this->purchase_model->get_customer();
			$this->data['branches']=$this->purchase_model->get_branch();
			if($this->data['suppliers']==''){
				$this->data['suppliers']=array();
				}
			$id_branch=$this->session->userdata('id_branch');
			$meta['page_title'] = "Add Purchase";
	   		$branch=$this->purchase_model->get_invoicecode($id_branch)->row_array();
			$profile=$this->purchase_model->get_profile()->row_array();
			
		if (isset($_POST) && !empty($_POST))
		{
			// do we have a valid request?
			/*if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
			{
				show_error('This form post did not pass our security checks.');
			}*/

//			echo "i am here";exit;
			
			if($this->input->post('trans_name')!=''){
				$trans_name = $this->input->post('trans_name');
				}else{
					$trans_name ='';
				}
				
			
			if($this->input->post('trans_amt')!=''){
				
				$trans_amt = $this->input->post('trans_amt');
				}else{
					$trans_amt =0;
				}
			
			if($this->input->post('id_branch')!=0){
				$branch = $this->input->post('id_branch');
				}else{
					
					$branch=0;
				}
		
			$tx = $this->input->post('tx');
			$data = array(
				'referance_no' =>strtoupper( $this->input->post('referance_no')),
				'id_supplier' => $this->input->post('id_supplier'),
				'supplier_name' => strtoupper($this->purchase_model->get_cname($this->input->post('id_supplier'))),
				'amount' => $this->input->post('total_amount'),
				'total_disc' => $this->input->post('total_discount'),
				'total_tax' => $this->input->post('total_tax'),
				'total_balance' => $this->input->post('total_balance'),
				'round_off' => $this->input->post('round_off'),
				'total_price' => $this->input->post('total_price'),
				'transaction_type' => $this->input->post('type'),
				'referance_date' => $this->input->post('referance_date'),
				'received_date' => $this->input->post('received_date'),
				'id_branch' => $this->input->post('id_branch'),
				'transport_name' => strtoupper($trans_name),	
				'transport_amount' => $trans_amt,
				'final_price' => floatval($this->input->post('total_price'))+floatval($trans_amt)	
			);
			
			if($data['transaction_type']==0){
				$data['status']=1;
				
			}
			//
			//print_r($user->id);exit;
			//Update the groups user belongs to
			if ($this->form_validation->run() === TRUE)
			{
				$latest_id=$this->purchase_model->insert($data);
				$code = $this->input->post('code');
				$rate = $this->input->post('rate');
				$quantity = $this->input->post('quantity');
				$amount = $this->input->post('amount');
				$discount = $this->input->post('discount');
				$tax = $this->input->post('tax');
				$total = $this->input->post('total');
				$balance = $this->input->post('balance');
				$length = count($code);
					for($i=0; $i<$length; $i++){
						$new_data['id_purchase'] = $latest_id;
						$new_data['date'] = $data['received_date'];
						$new_data['id_product'] = $this->purchase_model->get_product_id($code[$i]);
						$new_data['quantity'] = $quantity[$i];
						$new_data['rate'] = $rate[$i];
						$new_data['amount'] = $amount[$i];
						$new_data['tax'] = $tax[$i];
						$new_data['discount'] = $discount[$i];
						$new_data['total'] = $total[$i];
						$new_data['balance'] = $balance[$i];
						$new_data['ip']  =  $_SERVER['REMOTE_ADDR'];	
						$this->purchase_model->insert_item($new_data,$branch);	
						}							
				//echo "welldone prabhas!!";exit;
				redirect("purchase/purchase_order/new_order");
			}
		}

		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

		
 		$this->data['referance_no'] = array(
			'name'  => 'referance_no',
			'id'    => 'referance_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('referance_no'),
		);

		
		$this->data['referance_date'] = array(
			'name'  => 'referance_date',
			'id'    => 'referance_date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('referance_date'),
			'class' => 'datepicker'
		);
		
		$this->data['received_date'] = array(
			'name'  => 'received_date',
			'id'    => 'received_date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('received_date'),
			'class' => 'datepicker'
		);

	$this->data['id_customer'] = array(
			'name'  => 'id_customer',
			'id'    => 'id_customer',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('id_customer'),
		);
		
			$this->data['taxes']=$this->purchase_model->get_tax();
			$this->data['excises']=$this->purchase_model->get_excise();
			$this->load->view('common/header', $meta);
			$this->load->view('purchase-order/new_order', $this->data);
			$this->load->view('common/footer');	   
	}
		
		
	
	function get_product(){
	 $rate=$this->purchase_model->get_product($_REQUEST['code'])->row_array();
		if(count($rate)>0){
		echo $rate['purchase_price'];exit;
		}else{
			echo 1;
		}
	
	}



function view_purchase(){
	
		$this->data['users'] = $this->purchase_model->get_purchaseList()->result();
		//print_r($this->data['users']);exit;
		$meta['page_title'] = "Purchases";
	   $this->data['message'] = "Purchases";
	   $this->load->view('common/header', $meta);
	   $this->load->view('purchase/view_order1');
       $this->load->view('common/footer');	
	
}

function view_unique_purchase($id){
$meta['page_title']="purchase Details";
$this->data['referance_no']= $this->purchase_model->get_ref($id);
$this->data['users']= $this->purchase_model->get_uniquepurchase($id)->result();
	$this->load->view('common/header', $meta);
$this->load->view('purchase/view_unique_order',$this->data);
$this->load->view('common/footer');
}


function edit_purchase($id){
	
			if (!$this->ion_auth->logged_in())
			{
				redirect('auth', 'refresh');
			}
			
			$user = $this->purchase_model->edit_purchase($id)->row();
			
			$products = $this->purchase_model->edit_products($id)->result();

			$this->form_validation->set_rules('referance_no', 'Referance Number', 'required|xss_clean');
			$this->form_validation->set_rules('referance_date', 'Referance date', 'required|xss_clean');
			$this->form_validation->set_rules('received_date', 'Received date', 'required|xss_clean');
			$this->form_validation->set_rules('id_supplier', 'Supplier', 'required|xss_clean');
		
			$this->data['taxes']=$this->purchase_model->get_tax();
			if(count($this->data['taxes']) == 0){
				$this->data['taxes'] = array();
				
			}
			$this->data['suppliers']=$this->purchase_model->get_customer();
			$this->data['branches']=$this->purchase_model->get_branch();
			if(count($this->data['branches'])== 0){
				$this->data['branches'] = array();
				
			}
			if($this->data['suppliers']==''){
				$this->data['suppliers']=array();
			}
			$id_branch=$this->session->userdata('id_branch');
			$meta['page_title'] = "Edit Purchase";
	   		$profile=$this->purchase_model->get_profile()->row_array();
			
		if (isset($_POST) && !empty($_POST))
		{
			
			if($this->input->post('trans_name')!=''){
				
				$trans_name = $this->input->post('trans_name');
				}else{
					$trans_name ='';
				}
				
			
			if($this->input->post('trans_amt')!=''){
				
				$trans_amt = $this->input->post('trans_amt');
				}else{
					$trans_amt =0;
				}
				if($this->input->post('id_branch')!=0){
				$branch = $this->input->post('id_branch');
				}else{
					
					$branch=0;
				}
			
			
		
			$tx = $this->input->post('tx');
			$data = array(
				'referance_no' => $this->input->post('referance_no'),
				'id_supplier' => $this->input->post('id_supplier'),
				'supplier_name' => strtoupper($this->purchase_model->get_cname($this->input->post('id_supplier'))),
				'amount' => $this->input->post('total_amount'),
				'total_disc' => $this->input->post('total_discount'),
				'total_tax' => $this->input->post('total_tax'),
				'round_off' => $this->input->post('round_off'),
				'total_price' => $this->input->post('total_price'),
				'total_balance' => $this->input->post('total_balance'),
				'transaction_type' => $this->input->post('type'),
				'id_branch' => $this->input->post('id_branch'),
				'referance_date' => $this->input->post('referance_date'),
				'received_date' => $this->input->post('received_date'),
				'transport_name' => strtoupper($trans_name),	
				'transport_amount' => $trans_amt,
				'final_price' => floatval($this->input->post('total_price'))	
			);
			
			if ($this->form_validation->run() === TRUE)
			{
				//print_r($data);exit;
				$this->purchase_model->update($data,$id);
				$id_branch=$this->input->post('id_branch');
				$purchase_product_id = $this->input->post('purchase_product_id');
				$code = $this->input->post('code');
				$rate = $this->input->post('rate');
				$quantity = $this->input->post('quantity');
				$amount = $this->input->post('amount');
				$discount = $this->input->post('discount');
				$tax = $this->input->post('tax');
				$balance = $this->input->post('balance');
				$total = $this->input->post('total');
				$length = count($code);
					for($i=0; $i<$length; $i++){
						$id_pur_product = $purchase_product_id[$i];
						$new_data['id_purchase'] = $id;
						$new_data['date'] = $data['received_date'];
						$new_data['id_product'] = $this->purchase_model->get_product_id($code[$i]);
						$new_data['quantity'] = $quantity[$i];
						$new_data['rate'] = $rate[$i];
						$new_data['amount'] = $amount[$i];
						$new_data['tax'] = $tax[$i];
						$new_data['discount'] = $discount[$i];
						$new_data['total'] = $total[$i];
						$new_data['balance'] = $balance[$i];
						$new_data['ip']  =  $_SERVER['REMOTE_ADDR'];
						//print_r($new_data);exit;
						$this->purchase_model->update_item($new_data,$id_branch,$id_pur_product,$user->id_branch);
                       					
						}
															
				redirect("purchase/purchase_order/view_order");
		}
		}

		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

		
 		$this->data['referance_no'] = array(
			'name'  => 'referance_no',
			'id'    => 'referance_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('referance_no',$user->referance_no),
		);

		$this->data['referance_date'] = array(
			'name'  => 'referance_date',
			'id'    => 'referance_date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('date',$user->referance_date),
			'class' => 'datepicker'
		);
		$this->data['received_date'] = array(
			'name'  => 'received_date',
			'id'    => 'received_date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('received_date',$user->received_date),
			'class' => 'datepicker'
		);


	$this->data['id_customer'] = array(
			'name'  => 'id_customer',
			'id'    => 'id_customer',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('id_customer',$user->id_supplier),
		);
		$this->data['products'] = $products;
		$this->data['user'] = $user;
			$this->data['taxes']=$this->purchase_model->get_tax();
			$this->load->view('common/header', $meta);
			$this->load->view('purchase/edit_order', $this->data);
			$this->load->view('common/footer');
	
	
}




function delete_purchase($id){
	
$this->purchase_model->delete($id);
redirect("purchase/purchase/view_order");

}



}

?>
